package com.liushuai.phoneguardian;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

/**
 * 毛玻璃 设置归属地
 * 
 * @author liushuai
 * 
 */
public class DragViewActivity extends Activity {

	private ImageView iv_drag_view;
	private Display display;
	private TextView tv_drag_view;
	private SharedPreferences sp;
	long firstClickTime = 0;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
				WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		setContentView(R.layout.activity_drag_view);
		iv_drag_view = (ImageView) findViewById(R.id.iv_drag_view);
		tv_drag_view = (TextView) findViewById(R.id.tv_drag_view);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
		display = wm.getDefaultDisplay();

		int lastx = sp.getInt("lastx", 0);
		int lasty = sp.getInt("lasty", 0);

		RelativeLayout.LayoutParams params = (LayoutParams) iv_drag_view
				.getLayoutParams();
		params.leftMargin = lastx;
		params.topMargin = lasty;
		iv_drag_view.setLayoutParams(params);

		iv_drag_view.setOnTouchListener(new OnTouchListener() {
			int startX = 0;
			int startY = 0;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					break;

				case MotionEvent.ACTION_MOVE:
					int newX = (int) event.getRawX();
					int newY = (int) event.getRawY();
					int dx = newX - startX;
					int dy = newY - startY;
					int l = iv_drag_view.getLeft();
					int t = iv_drag_view.getTop();
					int r = iv_drag_view.getRight();
					int b = iv_drag_view.getBottom();

					int newl = l + dx;
					int newr = r + dx;
					int newt = t + dy;
					int newb = b + dy;

					// 防止移出屏幕
					if (newl < 0 || newr > display.getWidth() || newt < 0
							|| newb > (display.getHeight() - 30)) {
						break;
					}

					int tv_height = tv_drag_view.getBottom()
							- tv_drag_view.getTop();

					if (newt > display.getHeight() / 2) {
						// 图片在下面 文本设置在上面
						tv_drag_view.layout(tv_drag_view.getLeft(), 0,
								tv_drag_view.getRight(), tv_height);
					} else {
						tv_drag_view.layout(tv_drag_view.getLeft(),
								display.getHeight() - tv_height - 30,
								tv_drag_view.getRight(),
								display.getHeight() - 30);
					}

					iv_drag_view.layout(newl, newt, newr, newb);
					// 重新初始化是指位置
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();

					break;

				case MotionEvent.ACTION_UP:

					Editor et = sp.edit();
					et.putInt("lastx", iv_drag_view.getLeft());
					et.putInt("lasty", iv_drag_view.getTop());
					et.commit();

					break;
				}
				return false;
			}
		});

		iv_drag_view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (firstClickTime > 0) {
					long secondClickTime = System.currentTimeMillis();
					if ((secondClickTime - firstClickTime) < 500) {
						// 双击事件
						int iv_width = iv_drag_view.getRight() - iv_drag_view.getLeft();
						int l = display.getWidth()/2 - iv_width/2;
						int r = display.getWidth()/2 + iv_width/2;
						iv_drag_view.layout(l, iv_drag_view.getTop(), r, iv_drag_view.getBottom());
						
						Editor editor = sp.edit();
						editor.putInt("lastx", iv_drag_view.getLeft());
						editor.putInt("lasty", iv_drag_view.getTop());
						editor.commit();
					}
				}
				// 判断是否是第一次点击 记录当前时间
				firstClickTime = System.currentTimeMillis();
				new Thread() {
					public void run() {
						try {
							Thread.sleep(500);

						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						firstClickTime = 0;
					};
				}.start();
			}
		});
	}
}
